<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('权限修改')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="resId"
                               th:value="${sysMenu.resId}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级</label>
                    <div class="layui-input-block">
                        <ul class="dtree" data-id="-1" id="selectParent" name="parentId"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="resName"
                               placeholder="权限名称" th:value="${sysMenu.resName}" type="text">
                    </div>
                </div>
                <div class="layui-form-item" id="resCodeItem">
                    <label class="layui-form-label">标识</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" id="resCode" name="resCode"
                               placeholder="权限标识" th:value="${sysMenu.resCode}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input lay-filter="resType" name="resType" th:checked="${sysMenu.resType eq '0'}"
                               title="目录" type="radio" value="0">
                        <input lay-filter="resType" name="resType" th:checked="${sysMenu.resType eq '1'}"
                               title="菜单" type="radio" value="1">
                        <input lay-filter="resType" name="resType" th:checked="${sysMenu.resType eq '2'}"
                               title="按钮" type="radio" value="2">
                    </div>
                </div>
                <div class="layui-form-item" id="resUrlItem">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" id="resUrl" name="resUrl"
                               placeholder="菜单路径" th:value="${sysMenu.resUrl}" type="text">
                    </div>
                </div>
                <div class="layui-form-item" id="openTypeItem">
                    <label class="layui-form-label">打开</label>
                    <div class="layui-input-block">
                        <select id="openType" name="openType">
                            <option value=""></option>
                            <option th:selected="${sysMenu.openType eq '_iframe'}" value="_iframe">框架</option>
                            <option th:selected="${sysMenu.openType eq '_blank'}" value="_blank">签页</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="icon" lay-filter="icon" name="icon" th:value="${sysMenu.icon}"
                               type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="sort" placeholder="排序"
                               th:value="${sysMenu.sort}" type="text">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="res-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="pear-btn pear-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'iconPicker', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let iconPicker = layui.iconPicker;
        let dtree = layui.dtree;

        dtree.renderSelect({
            elem: "#selectParent",
            url: "/sys/menu/selectParent",
            method: 'get',
            selectInputName: {nodeId: "parentId", context: "resName"},
            skin: "layui",
            dataFormat: "list",
            response: {treeId: "resId", parentId: "parentId", title: "resName"},
            selectInitVal: [[${sysMenu.parentId}]]
        });

        form.on("radio(resType)", function (data) {
            if (this.value == '0') {
                $("#resUrlItem").hide();
                $("#resCodeItem").hide();
                $("#openTypeItem").hide();
                $("#resUrl").val("");
                $("#resCode").val("");
                $("#openType").val("");
            } else if (this.value == '1') {
                $("#resUrlItem").show();
                $("#resCodeItem").show();
                $("#openTypeItem").show();
            } else if (this.value == '2') {
                $("#resUrlItem").hide();
                $("#openTypeItem").hide();
                $("#resCodeItem").show();
                $("#resUrl").val("");
                $("#openType").val("");
            }
        });

        form.on('submit(res-save)', function (data) {
            data.field.icon = "layui-icon " + data.field.icon;
            $.ajax({
                url: '/sys/menu/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.render();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        iconPicker.render({
            elem: '#icon',
            type: 'fontClass',
            search: true,
            page: true,
            limit: 12,
            click: function (data) {
                console.log(data);
            }
        });

        iconPicker.checkIcon("icon", [[${sysMenu.icon}]]);

        window.init = function (type) {
            if (type == '0') {
                $("#resUrlItem").hide();
                $("#resCodeItem").hide();
                $("#openTypeItem").hide();
                $("#resUrl").val("");
                $("#resCode").val("");
                $("#openType").val("");
            } else if (type == '1') {
                $("#resUrlItem").show();
                $("#resCodeItem").show();
                $("#openTypeItem").show();
            } else if (type == '2') {
                $("#resUrlItem").hide();
                $("#openTypeItem").hide();
                $("#resCodeItem").show();
                $("#resUrl").val("");
                $("#openType").val("");
            }
        }

        window.init([[${sysMenu.resType}]]);
    })
</script>
<script>
</script>
</body>
</html>